package cx3k.GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

import cx3k.dataaccess.search.Searcher;
import cx3k.scheduler.Scheduler;
import cx3k.scheduler.objects.Course;

public class ConnectFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JButton jbExit;
	private Scheduler scheduler;
	private Searcher searcher;


	public ConnectFrame(Scheduler scheduler, Searcher searcher) {
		this.setTitle("Connect");
		//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setSize(400, 200);
		this.setResizable(false);
		this.scheduler = scheduler;
		this.searcher = searcher;
		
		JTextArea jlText = new JTextArea("We were unable to connect to the Data Service.\nPlease Connect to the internet.");
		this.add(jlText, BorderLayout.CENTER);
		
		jbExit = new JButton("Exit");
		jbExit.addActionListener(new ExitListener());
		
		JPanel jpButtons = new JPanel();
		jpButtons.add(jbExit);
		this.add(jpButtons, BorderLayout.SOUTH);
		
		
		//Center frame on screen
		Toolkit toolkit = this.getToolkit();
	    Dimension size = toolkit.getScreenSize();
	    this.setLocation(size.width/2 - getWidth()/2, size.height/2 - getHeight()/2);
		this.setVisible(true);
	}
	
	private class ExitListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			System.exit(0);
		}
		
	}
	
	public Object[] waitForConnect(){
		boolean b = true;
		while (b) {
			try {
				System.out.println("Trying to connect");
				this.scheduler = new Scheduler();
				this.searcher = new Searcher(Course.class);
				b = false;
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
		Object[] o = new Object[2];
		o[0] = scheduler;
		o[1] = searcher;
		return o;
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
	}
}
